package com.tlgen.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.tlgen.R;
import com.tlgen.bean.Credit;

import java.util.ArrayList;

public class LetterRecyclerViewAdapter extends RecyclerView.Adapter<LetterRecyclerViewAdapter.myViewHodler> {

    private Context context;
    private ArrayList<Credit> creditArrayList;

    //创建构造函数
    public LetterRecyclerViewAdapter(Context context, ArrayList<Credit> creditArrayList) {
        //将传递过来的数据，赋值给本地变量
        this.context = context;//上下文
        this.creditArrayList = creditArrayList;//实体类数据ArrayList
    }

    @Override
    public LetterRecyclerViewAdapter.myViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        //创建自定义布局
        View itemView = View.inflate(context, R.layout.fragment_letter_item, null);
        return new LetterRecyclerViewAdapter.myViewHodler(itemView);
    }

    /**
     * 绑定数据，数据与view绑定
     */
    @Override
    public void onBindViewHolder(LetterRecyclerViewAdapter.myViewHodler holder, int position) {
        //根据点击位置绑定数据
        Credit data = creditArrayList.get(position);

        // glide加载字符串图片
        Glide.with(context).load("http://124.70.180.155:8080" + data.getLetterOfAdmission()).into(holder.letter);
    }

    /**
     * 得到总条数
     */
    @Override
    public int getItemCount() {
        return creditArrayList.size();
    }

    class myViewHodler extends RecyclerView.ViewHolder {
        private ImageView letter;

        public myViewHodler(View itemView) {
            super(itemView);
            letter = (ImageView) itemView.findViewById(R.id.iv_letter);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "点击了" + v.getId(), Toast.LENGTH_LONG).show();
                    if(onItemClickListener != null){
                        onItemClickListener.OnItemClick(v, creditArrayList.get(getLayoutPosition()));
                    }
                }
            });

        }
    }

    /**
     * 设置item的监听事件的接口
     */
    public interface OnItemClickListener {
        /**
         * 接口中的点击每一项的实现方法，参数自己定义
         *  @param view 点击的item的视图
         * @param data 点击的item的数据
         */
        public void OnItemClick(View view, Credit data);
    }

    //需要外部访问，所以需要设置set方法，方便调用
    private LetterRecyclerViewAdapter.OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(LetterRecyclerViewAdapter.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}